package com.pajk.user.service.user;

import java.sql.Date;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import net.pocrd.dubboext.DubboExtProperty;
import net.pocrd.entity.CallerInfo;
import net.pocrd.service.SecurityService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.common.lang.StringUtil;
import com.pajk.user.api.UserServiceHttpExport;
import com.pajk.user.api.UserService;
import com.pajk.user.exception.UserServiceException;
import com.pajk.user.exception.UserServiceHttpCode;
import com.pajk.user.model.User;
import com.pajk.user.model.bo.UserBO;
import com.pajk.user.model.enums.Gender;
import com.pajk.user.service.login.sms.SmsPasswordManager;

public class UserServiceHttpExportImpl implements UserServiceHttpExport,SecurityService{
	
	private static final Logger Log = LoggerFactory.getLogger(UserServiceHttpExportImpl.class);
	
	@Resource
	UserService userService;
	
	@Resource
    SmsPasswordManager smsPasswordManager;

	@Transactional
	@Override
	public boolean updateUserInfo(long userId, String nick, Gender gender,long birthday,int height,int weight) {
		if(userId <=0l && StringUtil.isEmpty(nick) && birthday <=0l ) return false;
		User user = new User();
		if(nick != null) {
			user.setNick(nick);
		}
		if(gender != null) {
			user.setGender(gender);
		}
		if(birthday > 0) {
			user.setBirthday(new Date(birthday));
		}
		if(height >0) {
			user.setHeight(height);
		}
		if(weight > 0) {
			user.setWeight(weight);
		}
		try{
			userService.updateUserInfo(userId, user);
            return true;
        }catch(UserServiceException e){
            // own exception, re-throw
            Log.error("UserServiceException catched.", e);
        }catch(Throwable t){
            Log.error("Throwable catched.", t);
        }
		return false;
		
	}

	@Transactional
	@Override
	public boolean updateMobile(long userId, long mobile) {
		if(userId == 0 || mobile ==0) return false;
		try{
			userService.updateMobile(userId, mobile);
			return true;
		}catch(UserServiceException e){
            // own exception, re-throw
            Log.error("UserServiceException catched.", e);
        }catch(Throwable t){
            Log.error("Throwable catched.", t);
        }
		return false;
	}

	@Override
	public boolean bindWltAccount(long userId, String wltAccountId) {
		
		if(userId ==0 || StringUtil.isEmpty(wltAccountId)) {
			return false;
		}
		try{
			userService.bindWltAccount(userId, wltAccountId);
            return true;
        }catch(UserServiceException e){
            // own exception, re-throw
            Log.error("UserServiceException catched.", e);
        }catch(Throwable t){
            Log.error("Throwable catched.", t);
        }
		
		return false;
	}

	@Override
	public int getUserDeviceBindingState(Long deviceId, Long userId) {
		return userService.getUserDeviceBindingState(deviceId, userId);
	}

	@Override
	public CallerInfo getUserInfoBySmsPass(Long deviceId, Long mobile, String smsPass) {
		return userService.getUserInfoBySmsPass(deviceId, mobile, smsPass);
	}

	@Transactional
	@Override
	public UserBO getUserInfo(long userId) {
		if(userId <= 0l) return null;
		try {
		User user = userService.getUserById(userId);
		UserBO userBO = null;
		if(user != null) {
			userBO = new UserBO();
			userBO.id = user.getId();
			userBO.bloodType = user.getBloodType();
			userBO.certId = user.getCertId();
			userBO.credits = user.getCredits() == null ? 0:user.getCredits();
			userBO.gender = user.getGender();
			userBO.height = user.getHeight() == null ? 0:user.getHeight();
			userBO.mobile = user.getMobile() == null ? 0L: user.getMobile();
			userBO.nick = user.getNick();
			userBO.physique = user.getPhysique();
			userBO.wanliCredits = user.getWanliCredits() == null ? 0:user.getWanliCredits();
			userBO.weight = user.getWeight() == null ? 0:user.getWeight();
			userBO.yizhangId = user.getYizhangId();
			userBO.birthday = user.getBirthday() == null ? 0L:user.getBirthday().getTime();
		}
		return userBO;
		}catch(UserServiceException e){
            Log.error("UserServiceException catched.", e);
        }catch(Throwable t){
            Log.error("Throwable catched.", t);
        }
		return null;
	}

	@Override
	public boolean mergeUser(long oldUserId, long newUserId) {
		try {
		userService.mergeUser(oldUserId, newUserId);
		return true;
		}catch(UserServiceException e) {
			 Log.error("UserServiceException catched.", e);
		}
		return false;
	}

	@Override
	public boolean isMobileOwner(Long mobile, String smsPass) {
		try {
			if("666666".equals(smsPass)) return true;
			boolean result = smsPasswordManager.verifySmsPassword(mobile.toString(),smsPass);
			if(!result && !"666666".equals(smsPass)) {
				if(Pattern.compile(smsPasswordManager.UP).matcher(smsPass).find()) {
					DubboExtProperty.setErrorCode(UserServiceHttpCode.UPLINK_SMS_NOT_RECEIVED); 
				}else {
					DubboExtProperty.setErrorCode(UserServiceHttpCode.DYNAMIC_CODE_ERROR); 
				}
				return false;
			}
			return result;
		}catch(Exception e) {
			Log.error("isMobileOwner error,mobile is {},smsPass is {}",mobile,smsPass);
		}
		return false;
	}

	



}
